package com.qiniu;

import com.qiniu.android.storage.UpCancellationSignal;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;

/**
 * 七牛云存储工具类
 * @author ghordon
 * @since 2015-5-11 15:34:49
 */
public class QiniuUtils {

	private static void upload(String data, String key, String token,
			UpCompletionHandler completionHandler, UploadOptions uploadOptions) {
		try {
			new UploadManager().put(data, key, token, completionHandler,
					uploadOptions);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文件上传到七牛云
	 * 
	 * @param data
	 *            File对象、或 文件路径、或 字节数组
	 * @param key
	 *            保存在服务器上的资源唯一标识
	 * @param token
	 *            服务器分配的token
	 * @param completionHandler
	 *            上传完成后回调函数,必填
	 * @param progressHandler
	 *            上传进度回调，可为null
	 * @param cancellationSignal
	 *            取消上传，当isCancelled()返回true时，不再执行更多上传
	 */
	public static void upload(String data, String key, String token,
			UpCompletionHandler completionHandler,
			UpProgressHandler progressHandler,
			UpCancellationSignal cancellationSignal) {
		try {
			upload(data, key, token, completionHandler, new UploadOptions(null,
					null, false, progressHandler, cancellationSignal));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文件上传到七牛云
	 * 
	 * @param data
	 *            File对象、或 文件路径、或 字节数组
	 * @param key
	 *            保存在服务器上的资源唯一标识
	 * @param token服务器分配的token
	 * @param completionHandler上传完成后回调函数
	 *            ,必填
	 */
	public static void upload(String data, String key, String token,
			UpCompletionHandler completionHandler) {
		upload(data, key, token, completionHandler, null);
	}

	/**
	 * 获取指定宽和高的图片
	 * 
	 * @param host
	 *            域名
	 * @param key
	 *            对应上传的key
	 * @param width
	 *            图片的
	 * @param height
	 * @return
	 */
	public static String getImageUrl(String host, String key, int width,
			int height) {
		return host + "/" + key + "?imageView2/1/w/" + width + "/h" + height;
	}

}
